//保存主表
function saveBase(){
	if(checkRequire('addPEForm') != "Success")return;
	$.ajax({
		url:'../../printExplain/saveBase',
		data:$('#addPEForm').serialize(),
		success:function(data){
			sucReturnObj(data,'addPEForm input[name=peId]');
		}
	});
}

//保存封面印刷
function saveFM(){
	saveObjs('#addPEForm input[name=peId]',
			'peFengmianId',
			'../../printExplain/saveFM',
			'#fmPanelAdd',
			'FMYS_FORM_',
			'FMYS');
}

//保存封面正文印刷
function saveZW(){
	saveObjs('#addPEForm input[name=peId]',
			'peFengmianId',
			'../../printExplain/saveZW',
			'#zwPanelAdd',
			'ZWYS_FORM_',
			'ZWYS');
}

//保存封面装订项目
function saveZD(){
	saveObjs('#addPEForm input[name=peId]',
			'peFengmianId',
			'../../printExplain/saveZD',
			'#zdPanelAdd',
			'ZDXM_FORM_',
			'ZDXM');
}

//保存通用方法(主表的id对象,保存的路径,所有表单的父Id,每个表单Form的范例Id,每个表table的范例Id)
function saveObjs(peIdObj,itemPkNameEx,url,panelId,formIdEx,tableIdEx){
	//先判断基本信息是否保存
	if($(peIdObj).val() == "" || (typeof($(peIdObj))=="undefined") ){
		$.messager.alert("提示框","请先保存印刷说明单基本信息","info");
		return;
	}
	
	//获取数组对象
	var printExplainFMs = new Array();
	var formobjLen = $(panelId+' table[id*='+tableIdEx+']').length;
	
	for(var i = 0; i < formobjLen; i++){
		if(checkRequire(formIdEx+''+i) != "Success")return;
		printExplainFMs[i] = getFormJson(formIdEx+''+i);
	}
	
	var peId = $(peIdObj).val(); 
	
	//保存数据
	$.ajax({
		url:url+'?peId='+peId,
		type:'post',
		contentType: "application/json;charset=utf-8",
		data:JSON.stringify(printExplainFMs),
		dataType: "json",
		success:function(data){
			sucReturnObj(data,'');
			//重新加载封面印刷
			//填充ids
			if(data.msg == "success"){
				var ids = data.objList;
				var tmpId = "";
				for(var i = 0; i < ids.length; i++){
					tmpId = $('#'+formIdEx+''+i+' input[name='+itemPkNameEx+']').val();
					if(tmpId != ""){
						if(tmpId != ids[i]){
							$.messager.alert("提示框","保存返回的ID不对");
						}else{
							continue;
						}
					}else{
						$('#'+formIdEx+''+i+' input[name='+itemPkNameEx+']').val(ids[i]);
					}
				}
			}
		}
	});
}
/*
 *详情中的主表基本信息的修改按钮，和点击修改之后加载的按钮 
 */
function prExInfoUpd(){
	wirtePart('upXTWin');
	$.ajax({
		type:'post',
		url:'../../framework/buttonsListC',
		data:{'butnId':'yzPrExInfoUpS'},
		success:function(data){
			if(data == ""){
				alert("按钮数据为空");
			}else{
				var buttons = data;
				var butHTML = "";
				for(var i = 0; i < buttons.length; i++){
					butHTML += formatButtons(buttons[i]);
				}
				$('#INFOPAGEBUT').html(butHTML);
				$.parser.parse('#INFOPAGEBUT');  //重新编译界面
			}
		}
	});
}
/**
 *详情中点击修改按钮之后的保存按钮执行的方法
 * @return
 */
function yzPrExInfoUpS(){
//	if(checkRequire('addPEForm') != "Success")return;
	var printExplain = getFormJson('upXTForm');
	$.ajax({
		type:'post',
		url:'../../printExplain/saveBase',
		contentType: "application/json;charset=utf-8",
		data:JSON.stringify(printExplain),
		dataType: "json",
		success:function(data){
			sucReturnObj(data,'upXTWin input[name=peId]');
		}
	});	
}
/**
 * 详情中的封面的修改按钮
 * @return
 */
function yzPrIExInfoUpd(){
	$('#fmPanel form[id*=fmys_form_]').each(function(){
		wirtePart($(this).attr('id'));
	});
	$.ajax({
		type:'post',
		url:'../../framework/buttonsListC',
		data:{'butnId':'yzPrExFmS'},
		success:function(data){
			if(data == ""){
				alert("按钮数据为空");
			}else{
				var buttons = data;
				var butHTML = "";
				for(var i = 0; i < buttons.length; i++){
					butHTML += formatButtons(buttons[i]);
				}
				$('#fmysButns').html(butHTML);
				$.parser.parse('#fmysButns');  //重新编译界面
			}
		}
	});
}
/**
 * 详情中封面印刷中的修改后的保存按钮
 * @return
 */
function yzPrExFmS(){
	saveObjs('#upXTForm input[name=peId]',
			'peFengmianId',
			'../../printExplain/saveFM',
			'#fmPanel',
			'fmys_form_',
			'FMYS');
	
}
/**
 * 详情中的正文的修改按钮
 * @return
 */
function yzPrZwUpd(){
	$('#zwPanel form[id*=zwys_form_]').each(function(){
		wirtePart($(this).attr('id'));
	});
	$.ajax({
		type:'post',
		url:'../../framework/buttonsListC',
		data:{'butnId':'yzPrExZwS'},
		success:function(data){
			if(data == ""){
				alert("按钮数据为空");
			}else{
				var buttons = data;
				var butHTML = "";
				for(var i = 0; i < buttons.length; i++){
					butHTML += formatButtons(buttons[i]);
				}
				$('#zwysButns').html(butHTML);
				$.parser.parse('#zwysButns');  //重新编译界面
			}
		}
	});
}
/**
 * 详情中正文印刷中的修改后的保存按钮
 * @return
 */
function yzPrExZwS(){
	saveObjs('#upXTForm input[name=peId]',
			'peFengmianId',
			'../../printExplain/saveZW',
			'#zwPanel',
			'zwys_form_',
			'ZWYS');
}
/**
 * 详情中装订项目中的修改按钮
 * @return
 */
function yzPrZdUpd(){
	$('#zdPanel form[id*=zdxm_form_]').each(function(){
		wirtePart($(this).attr('id'));
	});
	$.ajax({
		type:'post',
		url:'../../framework/buttonsListC',
		data:{'butnId':'yzPrExZdS'},
		success:function(data){
			if(data == ""){
				alert("按钮数据为空");
			}else{
				var buttons = data;
				var butHTML = "";
				for(var i = 0; i < buttons.length; i++){
					butHTML += formatButtons(buttons[i]);
				}
				$('#zdxmButns').html(butHTML);
				$.parser.parse('#zdxmButns');  //重新编译界面
			}
		}
	});
}
/**
 * 详情中装订项目中的修改后的保存按钮
 * @return
 */
function yzPrExZdS(){
	saveObjs('#upXTForm input[name=peId]',
			'peFengmianId',
			'../../printExplain/saveZD',
			'#zdPanel',
			'zdxm_form_',
			'ZDXM');
}
//生成综合施工单
function newExecut(){
	$('#exfmPanel').empty();
	$('#exzwPanel').empty();
	$('#exzdPanel').empty();
	var peId = $('#upXTForm input[name=peId]').val();
	$.ajax({
		type:'post',
		url:'../../printExplain/newExecut',
		data:{'peId':peId},
		success:function(data){
			if(data == ""){
				alert("按钮数据为空");
			}else{
				var listFm = data.objList[0];
				var listZw = data.objList[1];
				var listZd = data.objList[2];
				//一个综合施工单的实体类，还有三个明细
				//1.先window.open
				$("#EXItemInfo").window('open');
				//2.将施工单赋值，并弄成详情状态
				loadInfoEx(data.dynObj);
				//3.给每个明细赋值
				loadExFmInfo(listFm);
				loadExZwInfo(listZw);
				loadExZdInfo(listZd);
			}
		}
	});
}
/**
 * 加载封面的信息
 * @param fmList
 * @return
 */
function loadExFmInfo(fmList){
	$.ajax({
		type:'post',
		url:'../../framework/loadColumnsU',
		data:{'colId':'yz_execut_fm','type':'2'},
		success:function(data){
			if(fmList !=null && fmList.length>0){
				for(var f=0;f<fmList.length;f++){
					//创建panel
					$('#exfmPanel').append("<div id='exfm_"+f+"'><div title='封面印刷数据_"+f+"' class='easyui-panel' collapsible='true'><form id='exfmys_form_"+f+"'><table  id='EXFMYS_"+f+"'></table></form><hr/></div></div>");
					producePage(data,'EXFMYS_'+f,'2','');
					fillInfoByName('EXFMYS_'+f,fmList[f]);
					//全部变为不可写
					readOnlyAll('EXFMYS_'+f);
				}
				butnId='yzExFmAMod';
			}else{
				$('#exfmPanel').append("<div id='exfm_0'><div id='exfm_panel_0' collapsible='true'><form id='exfmys_form_0'><table  id='EXFMYS_0'></table></form><hr/></div></div>");
				producePage(data,'EXFMYS_0','2','');
				butnId='yzExFmUpdS';
			}
			$.ajax({
				type:'post',
				url:'../../framework/buttonsListC',
				data:{'butnId':butnId},
				success:function(data){
					if(data == ""){
						alert("按钮数据为空");
					}else{
						var buttons = data;
						var butHTML = "";
						for(var i = 0; i < buttons.length; i++){
							butHTML += formatButtons(buttons[i]);
						}
						$('#fmExButns').html(butHTML);
						$.parser.parse('#fmExButns');  //重新编译界面
					}
				}
			});
		}
	});
}
/**
 * 加载正文信息
 * @param zwList
 * @return
 */
function loadExZwInfo(zwList){
	$.ajax({
		type:'post',
		url:'../../framework/loadColumnsU',
		data:{'colId':'yz_execut_zw','type':'2'},
		success:function(data){
			if(zwList !=null && zwList.length>0){
				for(var f=0;f<zwList.length;f++){
					//创建panel
					$('#exzwPanel').append("<div id='exzw_"+f+"'><div title='正文印刷数据_"+f+"' class='easyui-panel' collapsible='true'><form id='exzwys_form_"+f+"'><table  id='EXZWYS_"+f+"'></table></form><hr/></div></div>");
					producePage(data,'EXZWYS_'+f,'2','');
					fillInfoByName('EXZWYS_'+f,zwList[f]);
					//全部变为不可写
					readOnlyAll('EXZWYS_'+f);
					butnId="yzExZwAMod";
				}
			}else{
				$('#exzwPanel').append("<div id='exzw_0'><div title='正文印刷数据_0' class='easyui-panel' collapsible='true'><form id='exzwys_form_0'><table  id='EXZWYS_0'></table></form><hr/></div></div>");
				producePage(data,'EXZWYS_0','2','');
				butnId="yzExZwUpdS";
			}
			$.ajax({
				type:'post',
				url:'../../framework/buttonsListC',
				data:{'butnId':butnId},
				success:function(data){
					if(data == ""){
						alert("按钮数据为空");
					}else{
						var buttons = data;
						var butHTML = "";
						for(var i = 0; i < buttons.length; i++){
							butHTML += formatButtons(buttons[i]);
						}
						$('#zwExButns').html(butHTML);
						$.parser.parse('#zwExButns');  //重新编译界面
					}
				}
			});
		}
	});
}
/**
 * 加载装订项目
 * */
function loadExZdInfo(zdList){
	$.ajax({
		type:'post',
		url:'../../framework/loadColumnsU',
		data:{'colId':'yz_execut_zd','type':'2'},
		success:function(data){
			if(zdList!=null && zdList.length>0){
				for(var f=0;f<zdList.length;f++){
					//创建panel
					$('#exzdPanel').append("<div id='exzd_"+f+"'><div title='装订项目数据_"+f+"' class='easyui-panel' collapsible='true'><form id='exzdxm_form_"+f+"'><table  id='EXZDXM_"+f+"'></table></form><hr/></div></div>");
					producePage(data,'EXZDXM_'+f,'2','');
					fillInfoByName('EXZDXM_'+f,zdList[f]);
					//全部变为不可写
					readOnlyAll('EXZDXM_'+f);
//					$.parser.parse("#zd_"+f);
				}
				butnId='yzExZdAMod';
			}else{
				$('#exzdPanel').append("<div id='exzd_0'><div title='装订项目数据_0' class='easyui-panel' collapsible='true'><form id='exzdxm_form_0'><table  id='EXZDXM_0'></table></form><hr/></div></div>");
				producePage(data,'EXZDXM_0','2','');
				butnId ='yzExZdUpdS';
//				$.parser.parse("#zd_0");
			}
			$.ajax({
				type:'post',
				url:'../../framework/buttonsListC',
				data:{'butnId':butnId},
				success:function(data){
					if(data == ""){
						alert("按钮数据为空");
					}else{
						var buttons = data;
						var butHTML = "";
						for(var i = 0; i < buttons.length; i++){
							butHTML += formatButtons(buttons[i]);
						}
						$('#zdExButns').html(butHTML);
						$.parser.parse('#zdExButns');  //重新编译界面
					}
				}
			});
		}
	});
}

function loadInfoEx(rowData){
	divName='upEXWin';//条件字段
	$.ajax({
        type:'post',
        url:'../../framework/loadColumnsU',
        data:{'colId':'yz_execut','type':'2'},
        success:function(data){
            if(data ==""){
                alert("数据为空");
            }else{
                //请求一个组织列的方法
                producePage(data,divName,'2','');
                
                fillInfoByName(divName,rowData);
            	//将字段变灰
            	readOnlyAll(divName);
            }
        }
     });
	$.ajax({
		type:'post',
		url:'../../framework/buttonsListC',
		data:{'butnId':'yzExInfoUpd'},
		success:function(data){
			if(data == ""){
				alert("按钮数据为空");
			}else{
				var buttons = data;
				var butHTML = "";
				for(var i = 0; i < buttons.length; i++){
					butHTML += formatButtons(buttons[i]);
				}
				$('#INFOEXBUT').html(butHTML);
				$.parser.parse('#INFOEXBUT');  //重新编译按钮
			}
		}
	});
}
function loadExInfowin(){
	$("#EXItemInfo").window({
		minWidth:950,
		height:550,
		maximized:true,
		collapsible: true,
		maximizable: true,
		title:'综合施工单',
		left:150,
		top:10
	});
	$("#EXItemInfo").window('close');
}